﻿#region Namespace References

using System;
using DynConDevCorp.OpenArch.SmartData.Interfaces;

#endregion

namespace DynConDevCorp.OpenArch.SmartData.Core.DisplayFormatters
{
    internal abstract class SmartDisplayFormatter : ISmartDisplayFormatter
    {
        public virtual void Dispatch(ISmartDispatcher dispatcher, object context)
        {
            dispatcher.ProcessDispatch(this, context);
        }

        #region ISmartDisplayFormatter Members

        public abstract string Format(object value, string formatString);

        public void SetOwningEntity(ISmartEntity entity)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region Public Instance Properties

        public string FormatString { get; set; }

        public ISmartEntity OwningEntity
        {
            get { throw new NotImplementedException(); }
        }

        public Type TargetType { get; protected set; }

        #endregion
    }

    internal class SmartDisplayFormatter<TData> : SmartDisplayFormatter
    {
        #region Public Instance Methods

        public override string Format(object rawValue, string formatString)
        {
            TData typedValue = (TData) rawValue;
            if (!string.IsNullOrEmpty(formatString))
            {
                string param = "{0:" + formatString + "}";
                return String.Format(param, rawValue);
            }
            else
            {
                if (typedValue != null)
                    return typedValue.ToString();
                else
                    return "{null}";
            }
        }

        #endregion
    }
}